package mrms.api.web.controllers;

import lombok.AllArgsConstructor;
import mrms.api.entity.DeptEntity;
import mrms.api.repository.DeptRepository;
import mrms.api.web.utils.Result;
import org.springframework.web.bind.annotation.*;

/**
 * @author lenchu
 * @date 2019/02/04
 */
@RestController
@AllArgsConstructor
public class DeptController {
    private final DeptRepository deptRepository;

    @GetMapping("/dept")
    public Result findAll() {
        return Result.success(deptRepository.findAll());
    }

    @GetMapping("/dept/{id}")
    public Result findById(@PathVariable String id) {
        return Result.success(deptRepository.findById(id).get());
    }

    @PostMapping("/a/dept")
    public Result save(@RequestBody DeptEntity deptEntity) {
        DeptEntity save = deptRepository.save(deptEntity);
        return Result.success(save);
    }

    @DeleteMapping("/a/dept/{id}")
    public Result delete(@PathVariable String id) {
        DeptEntity deptEntity = deptRepository.findById(id).get();
        deptRepository.delete(deptEntity);
        return Result.success(deptEntity);
    }
}
